package com.gdssoft.core.util;
/*
 *  捷达世软件(深圳)有限公司
 *  Copyright (C) 2008-2009 GuangZhou HongTian Software Company
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.Constants;

public class DateUtil {
	private static final Log logger = LogFactory.getLog(DateUtil.class);
	/**
	 * 设置当前时间为当天的最初时间（即00时00分00秒）
	 * 
	 * @param cal
	 * @return
	 */
	public static Calendar setStartDay(Calendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal;
	}

	public static Calendar setEndDay(Calendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal;
	}

	/**
	 * 把源日历的年月日设置到目标日历对象中
	 * @param destCal
	 * @param sourceCal
	 */
	public static void copyYearMonthDay(Calendar destCal,Calendar sourceCal){
		destCal.set(Calendar.YEAR, sourceCal.get(Calendar.YEAR));
		destCal.set(Calendar.MONTH, sourceCal.get(Calendar.MONTH));
		destCal.set(Calendar.DAY_OF_MONTH, sourceCal.get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 格式化日期为
	 * 
	 * @return
	 */
	public static String formatEnDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

		return sdf.format(date).replaceAll("上午", "AM").replaceAll("下午", "PM");
	}

	public static Date parseDate(String dateString) {
		Date date = null;
		try {
			date = DateUtils.parseDate(dateString, new String[]{Constants.DATE_FORMAT_FULL,Constants.DATE_FORMAT_YMD});
		} catch (Exception ex) {
			logger.error("Pase the Date(" + dateString + ") occur errors:"
					+ ex.getMessage());
		}
		return date;
	}
	
	/**
	 * 根据month字符串表示的日期年月（yyyy-MM），获取上月年月
	 * @author F3229233 2013-1-15 下午3:06:30 
	 * @param month
	 * @return 上月年月字符串
	 */
	public static String getLastMonthStr(String month) {
		Date date = dateParse(month,"yyyy-MM");
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		cd.add(Calendar.MONTH, -1);
		month = dateFormat(cd.getTime(),"yyyy-MM");
		return month;
	}
	/**
	 * 根据month字符串表示的日期年月（yyyy-MM），获取下月年月
	 * @author F3229233 2013-4-9 上午10:33:57 
	 * @param thisMonth
	 * @return
	 */
	public static String getNextMonthStr(String month) {
		Date date = dateParse(month,"yyyy-MM");
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		cd.add(Calendar.MONTH, 1);
		month = dateFormat(cd.getTime(),"yyyy-MM");
		return month;
	}
	
	/**
	 * 日期格式化
	 * @author F3229233 2013-1-17 上午8:53:44 
	 * @param d 日期 Date
	 * @return String
	 */
	public static String dateFormat(Date d){
		return dateFormat(d, Constants.DATE_FORMAT_FULL);
	}
	/**
	 * 日期格式化
	 * @author F3229233 2013-1-17 上午8:53:44 
	 * @param d 日期 Date
	 * @param pattern 如：'yyyy-MM' ...
	 * @return String
	 */
	public static String dateFormat(Date d,String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(d);
	}

	/**
	 * 字符串转换成日期
	 * @author F3229233 2013-1-17 上午8:55:38 
	 * @param stringDate
	 * @return Date
	 */
	public static Date dateParse(String stringDate){
		return dateParse(stringDate, Constants.DATE_FORMAT_FULL);
	}
	/**
	 * 字符串转换成日期
	 * @author F3229233 2013-1-17 上午8:55:38 
	 * @param stringDate
	 * @param pattern
	 * @return Date
	 */
	public static Date dateParse(String stringDate,String pattern){
		Date d;
		try {
			d = new SimpleDateFormat(pattern).parse(stringDate);
			return d;
		} catch (ParseException e1) {
			logger.error("将"+stringDate+"转换成"+pattern+"时发生错误！", e1);
		} catch (Exception e2) {
			logger.error("日期格式转换错误", e2);
		}
		return null;
	}

	
}
